package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.mapper.PurchaseSupplierMapper;
import com.intelligent.mapper.SupplierReconciliationMapper;
import com.intelligent.mapper.TUserMapper;
import com.intelligent.pojo.PurchaseSupplier;
import com.intelligent.pojo.SupplierReconciliation;
import com.intelligent.pojo.TUser;
import com.intelligent.service.SupplierReconciliationService;
import com.intelligent.vo.SupplierReconciliationPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;


@Service
public class SupplierReconciliationServiceImpl extends ServiceImpl<SupplierReconciliationMapper, SupplierReconciliation>
    implements SupplierReconciliationService{

    @Autowired
    private PurchaseSupplierMapper purchaseSupplierMapper;

    @Autowired
    private TUserMapper tUserMapper;

    @Override
    public Page<SupplierReconciliation> pageReconciliation(SupplierReconciliationPage req) {
        Page<SupplierReconciliation> page = new Page<>(req.getPageNum(), req.getPageSize());

        LambdaQueryWrapper<SupplierReconciliation> wrapper = new LambdaQueryWrapper<>();

        // 对账单号搜索
        if (req.getReconciliationNo() != null && !req.getReconciliationNo().trim().isEmpty()) {
            wrapper.like(SupplierReconciliation::getReconciliationNo, req.getReconciliationNo().trim());
        }

        // 供应商ID筛选
        if (req.getSupplierId() != null) {
            wrapper.eq(SupplierReconciliation::getSupplierId, req.getSupplierId());
        }

        // 状态筛选
        if (req.getStatus() != null) {
            wrapper.eq(SupplierReconciliation::getStatus, req.getStatus());
        }

        // 创建时间范围筛选
        if (req.getStartDate() != null && !req.getStartDate().isEmpty()) {
            LocalDate start = LocalDate.parse(req.getStartDate(), DateTimeFormatter.ISO_DATE);
            wrapper.ge(SupplierReconciliation::getCreateTime, java.sql.Date.valueOf(start));
        }
        if (req.getEndDate() != null && !req.getEndDate().isEmpty()) {
            LocalDate end = LocalDate.parse(req.getEndDate(), DateTimeFormatter.ISO_DATE);
            wrapper.le(SupplierReconciliation::getCreateTime, java.sql.Date.valueOf(end));
        }

        // 按创建时间倒序
        wrapper.orderByDesc(SupplierReconciliation::getCreateTime);

        this.page(page, wrapper);

        // 填充供应商名称 & 财务人员姓名
        for (SupplierReconciliation reconciliation : page.getRecords()) {
            if (reconciliation.getSupplierId() != null) {
                PurchaseSupplier supplier = purchaseSupplierMapper.selectById(reconciliation.getSupplierId());
                if (supplier != null) {
                    reconciliation.setSupplierName(supplier.getSupplierName());
                }
            }
            if (reconciliation.getUId() != null) {
                TUser user = tUserMapper.selectById(reconciliation.getUId());
                if (user != null) {
                    reconciliation.setFinanceStaffName(user.getRealName());
                }
            }
        }

        return page;
    }

    @Override
    public boolean createReconciliation(SupplierReconciliation reconciliation) {
        // 生成对账单号：GYDZ + yyyyMMdd + 4位自增ID
        String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String reconciliationNo = "GYDZ" + dateStr + String.format("%04d", (int)(Math.random() * 10000));
        reconciliation.setReconciliationNo(reconciliationNo);

        // 设置默认值
        if (reconciliation.getStatus() == null) {
            reconciliation.setStatus(0); // 默认待对账
        }
        reconciliation.setCreateTime(new Date());

        // 初始化金额字段
        if (reconciliation.getTotalPurchase() == null) {
            reconciliation.setTotalPurchase(java.math.BigDecimal.ZERO);
        }
        if (reconciliation.getTotalReceipt() == null) {
            reconciliation.setTotalReceipt(java.math.BigDecimal.ZERO);
        }
        if (reconciliation.getTotalPayment() == null) {
            reconciliation.setTotalPayment(java.math.BigDecimal.ZERO);
        }

        return this.save(reconciliation);
    }

    @Override
    public SupplierReconciliation getReconciliationDetail(Long id) {
        SupplierReconciliation reconciliation = this.getById(id);
        if (reconciliation != null && reconciliation.getSupplierId() != null) {
            PurchaseSupplier supplier = purchaseSupplierMapper.selectById(reconciliation.getSupplierId());
            if (supplier != null) {
                reconciliation.setSupplierName(supplier.getSupplierName());
            }
        }
        if (reconciliation != null && reconciliation.getUId() != null) {
            TUser user = tUserMapper.selectById(reconciliation.getUId());
            if (user != null) {
                reconciliation.setFinanceStaffName(user.getRealName());
            }
        }
        return reconciliation;
    }
}




